package com.thzy.bbs.google;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.support.v7.app.NotificationCompat;

import com.alibaba.fastjson.JSON;
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;
import com.thzy.bbs.R;
import com.thzy.bbs.activity.MainActivity;
import com.thzy.bbs.common.ActivityConfig;
import com.thzy.bbs.common.CommonUtils;
import com.thzy.bbs.common.Constants;
import com.thzy.bbs.entity.MessageNumber;
import com.thzy.bbs.thread.WebViewLoadThread;

import java.util.Map;

/**
 * fcm服务处理
 *
 * @author Lirui
 * @date 2017/1/3 16:20
 */

public class FCMService extends FirebaseMessagingService {
    /**
     * 接收到消息调用
     *
     * @param remoteMessage
     */
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        super.onMessageReceived(remoteMessage);
        generateNotification(this, remoteMessage);
    }

    /**
     * 删除消息时调用
     */
    @Override
    public void onDeletedMessages() {
        super.onDeletedMessages();
    }

    /**
     * 发送异常调用
     *
     * @param s
     * @param e
     */
    @Override
    public void onSendError(String s, Exception e) {
        super.onSendError(s, e);
    }

    @Override
    public void onMessageSent(String s) {
        super.onMessageSent(s);
    }


    /**
     * 推送消息处理
     */
    private void generateNotification(Context context, RemoteMessage message) {
        /**
         * 广播通知
         */
        NotificationManager notificationManager = (NotificationManager)
                context.getSystemService(Context.NOTIFICATION_SERVICE);
        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context);
        RemoteMessage.Notification notification = message.getNotification();
        mBuilder.setContentTitle(notification.getTitle())//消息标题
                .setSmallIcon(R.drawable.logo)//设置图标
                .setTicker(notification.getTitle())//通知首次出现在通知栏，带上升动画效果的
                .setStyle(new NotificationCompat.BigTextStyle())
                .setContentText(notification.getBody())//消息
                .setPriority(Notification.PRIORITY_HIGH)//高优先级用于重要的通信内容，例如短消息或者聊天，这些都是对用户来说比较有兴趣的。
                .setDefaults(Notification.DEFAULT_ALL)//默认手机设置方式提醒
                .setWhen(System.currentTimeMillis())//时间
                .setAutoCancel(true);//自动消失
        Intent broadcastIntent;
        broadcastIntent = new Intent(context, MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.
                getActivity(context, 0, broadcastIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        mBuilder.setContentIntent(pendingIntent);//设置跳转意图
        notificationManager.notify((int) System.currentTimeMillis(), mBuilder.build());

        /**
         * 推送至前端webVew页面及跳转
         */
        Map<String, String> data = message.getData();
        if (data.isEmpty()) {
            return;
        }
        String attention = data.get(ActivityConfig.fcmParameterName.HAVE_ATTENTION);
        boolean isHaveAttention = Boolean.parseBoolean(attention);
        //如果有关注，消息不用显示红点（反之有消息关注不显示）
        MessageNumber messageNumber = new MessageNumber(isHaveAttention, !isHaveAttention);
        if (MainActivity.isStart) {
            CommonUtils.webView.post(new WebViewLoadThread(CommonUtils.webView, Constants.JsFunctionName.INFORM_NUMBER, JSON.toJSONString(messageNumber)));
        }
    }
}
